package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dao.AnnualShareDataDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.AnnualShareData;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class AnnualShareDataDaoImpl extends
		AbstractDaoImpl<AnnualShareData, Integer> implements AnnualShareDataDao {

	protected AnnualShareDataDaoImpl() {
		super(AnnualShareData.class);
	}

	@Override
	public void saveAnnualShareData(AnnualShareData annualShareData) {
		saveOrUpdate(annualShareData);
	}

	@Override
	public List<AnnualShareData> findAnnualShareData(int companyId) {
		return findByCriteria(Restrictions.eq("companyId", companyId));
	}

	@Override
	public List<AnnualShareData> searchAnnualShareData(int companyId,
			int fromYear, int toYear, String orderBy, String orderByName) {
		Criteria criteria = getCurrentSession().createCriteria(
				AnnualShareData.class);

		criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
		criteria.addOrder(Order.desc(getRecordOrder(orderBy)));

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromYear > 0) {
			criteria.add(Restrictions.ge("year", fromYear));
		}

		if (toYear > 0) {
			criteria.add(Restrictions.le("year", toYear));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<AnnualShareData> searchAnnualShareData(int companyId,
			String companyName, int fromYear, int toYear, String orderBy,
			String orderByName, GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				AnnualShareData.class);

		if (orderBy != null) {
			criteria.addOrder(Order.desc(getRecordOrder(orderBy)));
			if (orderByName != null) {
				criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
			}
		} else {
			criteria.addOrder(Order.desc("date"));

			criteria.createAlias("company", "cmp");
			criteria.addOrder(Order.asc("cmp.code"));
		}

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (companyName != null) {
			criteria.createAlias("company", "comp");
			criteria.add(Restrictions.like("comp.name", companyName));
		}
		if (fromYear > 0) {
			criteria.add(Restrictions.ge("year", fromYear));
		}

		if (toYear > 0) {
			criteria.add(Restrictions.le("year", toYear));
		}

		return findByCriteria(criteria, gridPramDTO);
	}

	@Override
	@Transactional
	public void deleteAnnualShareData(int companyId, int fromYear, int toYear) {
		String sql = "delete from AnnualShareData where 1=1 ";
		if (companyId > Constants.ERROR) {
			sql += "and company_id = :companyId ";
		}
		if (fromYear > 0) {
			sql += "and year >= :fromYear ";
		}
		if (toYear > 0) {
			sql += "and year <= :toYear ";
		}

		Query query = getCurrentSession().createQuery(sql);
		if (companyId > Constants.ERROR) {
			query.setInteger("companyId", companyId);
		}
		if (fromYear > 0) {
			query.setParameter("fromYear", fromYear);
		}
		if (toYear > 0) {
			query.setParameter("toYear", toYear);
		}

		query.executeUpdate();
	}

}
